import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageTk
import numpy as np
import tensorflow as tf
from joblib import load
scaler = load('scaler.joblib')

# 加载训练好的模型
model1 = tf.keras.models.load_model('mnist_model1.h5')  # i1模型
model2 = tf.keras.models.load_model('mnist_model.h5')    # i2模型
model3= load('knn_mnist_model.joblib')
def classify_with_i1():
    file_path = filedialog.askopenfilename(filetypes=[("JPG files", "*.jpg")])
    if file_path:
        try:
            new_image = Image.open(file_path).convert('L')  # 转换为灰度
            new_image = new_image.resize((28, 28))  # 缩放到28x28
            new_image_array = np.array(new_image) / 255.0
            new_image_array = new_image_array.reshape(1, 28, 28, 1)  # 重塑并添加通道维度

            predictions = model1.predict(new_image_array)
            predicted_digit = np.argmax(predictions, axis=1)
            result_label.config(text=f"识别结果: {predicted_digit[0]}")
            messagebox.showinfo("识别结果", f"识别的数字是: {predicted_digit[0]}")

            photo = ImageTk.PhotoImage(new_image)
            img_label.config(image=photo)
            img_label.image = photo  # 避免图像被垃圾回收
        except Exception as e:
            messagebox.showerror("错误", f"方法1预测失败: {e}")

def classify_with_i2():
    file_path = filedialog.askopenfilename(filetypes=[("JPG files", "*.jpg")])
    if file_path:
        try:
            new_image = Image.open(file_path).convert('L')  # 转换为灰度
            new_image = new_image.resize((28, 28))  # 缩放到28x28
            new_image_array = np.array(new_image) / 255.0
            new_image_array = new_image_array.reshape(1, 28 * 28)  # 展开成784维向量

            predictions = model2.predict(new_image_array)
            predicted_digit = np.argmax(predictions, axis=1)
            result_label.config(text=f"识别结果: {predicted_digit[0]}")
            messagebox.showinfo("识别结果", f"识别的数字是: {predicted_digit[0]}")

            photo = ImageTk.PhotoImage(new_image)
            img_label.config(image=photo)
            img_label.image = photo  # 避免图像被垃圾回收
        except Exception as e:
            messagebox.showerror("错误", f"方法2预测失败: {e}")

def classify_with_i3():
    file_path = filedialog.askopenfilename(filetypes=[("JPG files", "*.jpg")])
    if file_path:
        try:
            new_image = Image.open(file_path).convert('L')  # 转换为灰度
            new_image = new_image.resize((28, 28))  # 缩放到28x28
            new_image_array = np.array(new_image) / 255.0
            new_image_array = new_image_array.reshape(1, 28 * 28)  # 展开成784维向量
            new_image_array = scaler.transform(new_image_array)
            predicted_digit = model3.predict(new_image_array)
            result_label.config(text=f"识别结果: {predicted_digit[0]}")
            messagebox.showinfo("识别结果", f"识别的数字是: {predicted_digit[0]}")

            photo = ImageTk.PhotoImage(new_image)
            img_label.config(image=photo)
            img_label.image = photo  # 避免图像被垃圾回收
        except Exception as e:
            messagebox.showerror("错误", f"方法3预测失败: {e}")

root = tk.Tk()
root.title("手写字符识别")
root.geometry("600x400")  # 设置窗口大小

label = tk.Label(root, text="请选择一张手写字符图片:")
label.pack()

classify_button_i1 = tk.Button(root, text="使用方法1识别图片", command=classify_with_i1)
classify_button_i1.pack()

classify_button_i2 = tk.Button(root, text="使用方法2识别图片", command=classify_with_i2)
classify_button_i2.pack()

classify_button_i3 = tk.Button(root, text="使用方法3识别图片", command=classify_with_i3)
classify_button_i3.pack()


img_label = tk.Label(root)
img_label.pack()

result_label = tk.Label(root, text="识别结果将显示在这里")
result_label.pack()

root.mainloop()